<?php
class Page {
	private $PageSize = 10; //每页记录数
	public $TotalPage; //总页数
	public $RecordNum; //总记录数
	public $NowPage; //记录总数
	private $QueryString; //执行的sql语句
	private $CycNum = 10; //默认显示数　10　条
	private $api;
	public function __construct($sql, $CycNum = 1) {
	    $pagesize = PAGESIZE;
	    if(!empty($pagesize)){$this->PageSize = $pagesize;}
		$this->api = new DbApi ( );
		if (is_numeric ( $CycNum )) {
			$this->CycNum = $CycNum;
		} else {
			$this->CycNum = $this->CycNum;
		}
		if (trim ( $sql ) != "") {
			if (preg_match ( "/limit/", $sql )) {
				list ( $sql, $limit ) = explode ( "limit", $sql );
			} else if (preg_match ( "/LIMIT/", $sql )) {
				list ( $sql, $limit ) = explode ( "LIMIT", $sql );
			}
			if (isset ( $limit )) {
				list ( $cnt1, $cnt2 ) = explode ( ",", $limit );
				if (! empty ( $cnt2 )) {
					$this->PageSize = $cnt2;
				} elseif (! empty ( $cnt1 )) {
					$this->PageSize = $cnt1;
				} else {
					$this->PageSize = $this->PageSize;
				}
			}
			$this->QueryString = $sql;
			unset ( $cnt1 );
			unset ( $cnt2 );
		}
	}
	
	public function StartPage($Sortid, $SortMethod = "down") {
		$this->RecordNum = $this->api->getNumRows ( $this->QueryString );
		$this->TotalPage = ceil ( $this->RecordNum / $this->PageSize );
		//排列顺序 up 升序 down 降序
		if (isset ( $SortMethod ) && $SortMethod == "up") {
			$SortMethod = "ASC";
		} elseif (isset ( $SortMethod ) && $SortMethod == "down") {
			$SortMethod = "DESC";
		} else {
			echo "<b>Warning</b>:<i>方法StartPage中的参数SortMethod只能是down或者up</i>";
		}
		if(isset($_REQUEST['NowPage'])){
		    $this->NowPage = intval($_REQUEST['NowPage']);
		}
		if (! isset ( $this->NowPage )) {
			$this->NowPage = 1;
		} elseif ($this->NowPage <= 0) {
			$this->NowPage = 1;
		} elseif ($this->NowPage > $this->TotalPage) {
			$this->NowPage = $this->TotalPage;
		} else {
			$this->NowPage = $this->NowPage;
		}
		$OffSet = $this->PageSize * ($this->NowPage - 1);
		$sql = $this->QueryString . " ORDER BY " . $Sortid . " " . $SortMethod . " LIMIT " . $OffSet . "," . $this->PageSize;
		return $sql;
	}
	
	public function EndPage($ButtonArray, $JumpType = "none", $IsDisNum = false) {
		$FirstPage = 1;
		$PrePage = $this->NowPage - 1;
		$NextPage = $this->NowPage + 1;
		$LastPage = $this->TotalPage;
		if (! is_array ( $ButtonArray ) or count ( $ButtonArray ) != 4) {
			$ButtonArray = array ("First", "Precede", "Next", "Last" );
		}
		$ReturnStr = "";
		$ReturnStr .= $this->ToPage ( $FirstPage, $ButtonArray [0], "First" );
		$ReturnStr .= " ";
		$ReturnStr .= $this->ToPage ( $PrePage, $ButtonArray [1], "Pre" );
		if ($IsDisNum === true) {
			$ReturnStr .= " " . $this->DisPageNum () . " ";
		} elseif ($IsDisNum === false) {
			$ReturnStr .= " ";
		} else {
			echo ('<b>Warning</b>:<i>方法EndPage()中参数$IsDisNum的类型是bool,只能是 true或者false</i><br>');
			$ReturnStr .= " ";
		}
		$ReturnStr .= $this->ToPage ( $NextPage, $ButtonArray [2], "Next" );
		$ReturnStr .= " ";
		$ReturnStr .= $this->ToPage ( $LastPage, $ButtonArray [3], "Last" );
		$ReturnStr .= " ";
		if ($JumpType === 'select') {
			$ReturnStr .= $this->JumpSelect ( 'select' );
		} elseif ($JumpType === 'text') {
			$ReturnStr .= $this->JumpSelect ( 'text' );
		} elseif ($JumpType === 'none') {
			//待写入
		} else {
			echo ('<b>Warning:</b><i>方法EndPage()中参数$JumpType 的值只能是 select text none </i><br>');
		}
		return $ReturnStr;
	}
	
	private function JumpSelect($JumpType) {
		$Url = $this->GetUrl ( 0 );
		if (preg_match ( "/\?/", $Url )) {
			$Url = $Url . "&NowPage";
		} else {
			$Url = $Url . "?NowPage";
		}
		$JumpString = "转到";
		if ($JumpType === 'select') {
			$JumpString .= "<select id='JumpSelect' name='NowPage' size='1'";
			$JumpString .= "onChange=\"window.location = '" . $Url . "='+this.value\">";
			for($i = 1; $i <= $this->TotalPage; $i ++) {
				if ($this->NowPage == $i) {
					$Extra = "selected";
				} else {
					$Extra = "";
				}
				$JumpString .= "<option value='" . $i . "' " . $Extra . ">" . $i . "</option>";
			}
			$JumpString .= "</select> 页";
		} elseif ($JumpType === 'text') {
			$Title = "鼠标外部单击，文章转向";
			$JumpString .= "<input id='JumpSelect' type='text' size='2' value='" . $this->NowPage . "'onBlur=\"";
			$JumpString .= "javascript:if(isNaN(parseInt(this.value)) || parseInt(this.value)";
			$JumpString .= " > " . $this->TotalPage . "){alert('您输入的数字只能在 1 ～ " . $this->TotalPage;
			$JumpString .= "之间，请重新输入'); return false;}location.href='" . $Url . "='+parseInt(this.value)\" title='" . $Title . "'>";
			$JumpString .= " 页";
		} else {
		
		}
		return $JumpString;
	}
	
	private function ToPage($Page, $Msg, $Flag = '') {
		$Url = $this->GetUrl ( $Page );
		$UrlStr = "<a class='pagelink' href='" . $Url . "'>" . $Msg . "</a>";
		if (($this->NowPage <= 1 and ($Flag == "First" or $Flag == "Pre")) or ($this->NowPage >= $this->TotalPage and ($Flag == "Next" or $Flag == "Last"))) {
			$UrlStr = "<span class='pagelink'>" . $Msg . "</span>";
		} else {
			//待写入
		}
		return $UrlStr;
	}
	
	private function GetUrl($Page) {
		if (! isset ( $_SERVER ['REQUEST_URI'] ))
			$request_url = $_SERVER ['SCRIPT_NAME'] . "?" . $_SERVER ['QUERY_STRING'];
		else
			$request_url = $_SERVER ['REQUEST_URI'];
		if ($_SERVER ['SERVER_PORT'] == 80) {
			$Url = "http://" . $_SERVER ['SERVER_NAME'] . $request_url;
		} else {
			$Url = "http://" . $_SERVER ['SERVER_NAME'] . ":" . $_SERVER ['SERVER_PORT'] . $request_url;
		}
		$Url = preg_replace ( "/\?NowPage=.*&?/i", "", $Url );
		$Url = preg_replace ( "/&NowPage=.*&?/i", "", $Url );
		if ($Page === 0) {
			return $Url;
		}
		if (preg_match ( "/\?/", $Url )) {
			$Url = $Url . "&NowPage=" . $Page;
		} else {
			$Url = $Url . "?NowPage=" . $Page;
		}
		return $Url;
	}
	
	private function DisPageNum() {
		if (! isset ( $PageNumString )) {
			$PageNumString = "";
		}
		for($i = $this->NowPage - $this->CycNum; $i < $this->NowPage + $this->CycNum; $i ++) {
			if ($i <= 0) {
				$i = 0;
				continue;
			} elseif ($i > $this->TotalPage) {
				break;
			} elseif ($i == $this->NowPage) {
				//$PageNum = "<font color=green>[" . $i . "]</font>";
				//$PageNumString .= $this->ToPage ( $i, $PageNum ) . " ";
			} else {
				//$PageNumString .= $this->ToPage ( $i, $i ) . " ";
			}
		}
		return $PageNumString;
	}
}

?>